﻿@model Jb.Web.ViewModels.CategoryViewModel
@using Jb.Web.Utility;

@{
    String pageTitle = "Edit Category";
    if (Model != null && Model.ID == 0)
    {
        pageTitle = "Add New Category";
    }
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = AdminMenu.ManageCategory;
    bool _HasSaved = ViewData.HasSaved();

    String _CategoryButtonText = "Select";//Model.ID > 0 ? "Change" : "Select";
    List<Jb.Web.ViewModels.CategoryViewModel> _Categories = ApplicationCache.Categories;
    String _CategoryMessage = "Select a Parent Category for this category. If you want to make this a Parent category then don't select anything.";
    if(Model.ParentID.HasValue){
        _CategoryMessage = "If you want to make this category a parent category then clear your selection and save the category information.";
    }
}

<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>Enter necessary information and then click the Save button.</div>
</div>
<div class="jb-page-content">
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    if (Model == null)
    {
        @: @Html.WarningMessageBox("Sorry! Requested Category was not found for edit.")
    }
    else
    {
        if (_HasSaved)
        {
            @Html.SuccessMessageBox("Congratulations! The Category saved successfully")
        }
        else if (ViewData[AppConstants.ViewDataKeys.SAVED] != null && !_HasSaved)
        {
            @Html.WarningMessageBox("Sorry! we could not save the Category.")
        }
		@Html.HiddenFor(model => model.ID)

		<div class="editor-label">
			@Html.LabelFor(model => model.Name)
		</div>
		<div class="editor-field">
			@Html.EditorFor(model => model.Name)
			@Html.ValidationMessageFor(model => model.Name)
            @Html.ValidationMessage("DuplicateCategory")
		</div>
        <div class="editor-label">
			@Html.LabelFor(model => model.Description)
		</div>
		<div class="editor-field">
            @Html.TextAreaFor(model => model.Description)				
			@Html.ValidationMessageFor(model => model.Description)
		</div>
        <div class="editor-label">
			@Html.LabelFor(model => model.ParentID)
            <div class="jb-label-hint">                
                @_CategoryMessage
            </div>
		</div>
        <div class="editor-field">				
            <span id="spanSelectedCategory">@if (Model.ParentID > 0)
                                            {@Html.GetCategoryBreadCrumb(Model.ParentID.GetValueOrDefault(), true)}</span>
            <button id="btnCategorySelection" type="button" value="" class="btn">@_CategoryButtonText</button>
            @Html.HiddenFor(model => model.ParentID)				
		</div>
		<div class="editor-label">
			@Html.LabelFor(model => model.PropertyDefinition)
            <div class="jb-label-hint">
                You can Provide a ZGL for this category. ZGLs are inherited by default from Parent category, but you can override them in child categories also.
            </div>
		</div>			
        <div class="editor-field">
            @Html.TextAreaFor(model => model.PropertyDefinition, new { @class = "jb-long-text" })		
            <div>
            @if (ViewData.ModelState.Values.Any(E => E.Errors.Count > 0))
            {
                var errors = ViewData.ModelState["PropertyDefinition"].Errors;
                if (errors != null && errors.Count > 0)
                {
                    <span class="field-validation-error">@Html.Raw(errors[0].ErrorMessage)</span>
                }
            }
            </div>
		</div>
		<div class="jb-button-container">
			<button class="btn btn-primary" type="submit" data-loading-text="Saving..."><i class="icon-hdd icon-white"></i> Save</button> 
			@Html.AdminLink("Back to Category List", "List", "Category", new { @class = "btn" })
		</div>
		        
        //Category Selection Dialog Box Start
        @*<div id="divCategoryDialog">            
            @Html.SelectableCategoryList(_Categories, true, Model.ParentID.GetValueOrDefault())
            <div class="clear"></div>  
            <div><a href="javascript:void(0);" onclick="ClearSelection();">Clear Selection</a></div>                    
        </div>*@
        
        <div id="divCategoryDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="divPhotoEditModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                <h4 id="mySFLabel">Select Category</h4>
            </div>
            <div class="modal-body jb-inline-label">
                <div id="divModalMesssage">
                    @Html.SelectableCategoryList(_Categories, true, Model.ParentID.GetValueOrDefault(), false)
                </div>
            </div>
            <div class="modal-footer">               
                <button type="button" class="btn btn-primary" onclick="SelectCategory();"><i class="icon-ok icon-white"></i> Select</button>
                <button type="button" class="btn" onclick="ClearSelection();"><i class="icon-refresh"></i> Clear Selection</button>
                <button type="button" class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i> Close</button>
            </div>
        </div>
        //Category Selection Dialog Box Ends
    }
}
</div>
@section JavaScriptPlaceHolder
{
    @Html.JavaScript("jquery.validate.min.js", Url)
    @Html.JavaScript("jquery.validate.unobtrusive.min.js", Url)
    <script language="javascript" type="text/javascript">        
        var _CategoryDialog = null;    
        $(document).ready(function () {            
            $('#btnCategorySelection').click(function(){
                //$('#divValidationMessage').hide(); $(_CategoryDialog).dialog('open');
                $('#divCategoryDialog').modal({ show: true });
            });
        });
        function GetCategoryBreadCrumb(categoryID){
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetCategoryBreadCrumb", "Ajax", new { area = AppConstants.Area.PUBLIC })',
                data: 'categoryID=' + categoryID,
                success: function (result) {
                    $('#spanSelectedCategory').html(result);                    
                }
            });
        }
        function SelectCategory() {            
            var categoryID = $('#divCategoryDialog input[type=radio]:checked').val();
            if(categoryID == undefined || categoryID == null || categoryID == 0){
                $('#ParentID').val(''); $('#spanSelectedCategory').html(''); $('#divCategoryDialog').modal('hide');
            }
            else
            {
                GetCategoryBreadCrumb(categoryID);
                $('#divCategoryDialog').modal('hide'); //$(_CategoryDialog).dialog('close');                
                $('#ParentID').val(categoryID);                
                //form.valid();                
            }
        }
        function ClearSelection(){
            $('#divCategoryDialog input[type=radio]:checked').attr('checked', false); 
            $('#ParentID').val('');
            $('#spanSelectedCategory').html('');
        }
    </script>
}

